local dataset = "$dataset" 
local samplenametext = "$samplenametext"
local samplenamefile = "$samplenamefile"

use "$results_fold/`dataset'.dta", clear


*capture drop *b4* *N4_*
preserve
drop if bs == 0 | bs == -1
drop *_F_* *_N2_* *_N3_*
collapse (sd) *
unab varl: *b2* *b3* //*b4*
foreach var of local varl {
    rename `var' se`var'
}
replace bs = 0
save "$working/temp/bstemp", replace
restore
preserve
drop *_F_* *_N2_* *_N3_*
keep if bs == -1
unab varl: *b2* *b3* //*b4*
foreach var of local varl {
    rename `var' se`var'
}
expand 3
save "$working/temp/bstemp2", replace
restore
keep if bs == 0
merge 1:1 bs using "$working/temp/bstemp"
drop _merge 
keep *b2* *b3* *N2_* *N3_* // *b4*
expand 13
append using "$working/temp/bstemp2"
gen id = _n
replace id = 1.5 if id == 14
replace id = 3.5 if id == 15
replace id = 5.5 if id == 16
drop bs
sort id
unab varlist: *b2* *b3* // *b4*
local noinst: subinstr local varlist "I_" "", all
local mainvars: subinstr local noinst "se" "", all
local mainvars: subinstr local mainvars "F_" "", all
local mainvars_uniq : list uniq mainvars
foreach v of varlist `mainvars_uniq' {
	replace `v' = . if id > 1 
	local temp "se`v'"
	replace `temp' = . if id != 2 & id != 1.5
}

unab varlist2: *I_b2*
local I2vars: subinstr local varlist2 "se" "", all
local I2vars: subinstr local I2vars "F_" "", all
local I2vars_uniq : list uniq I2vars
foreach v of varlist `I2vars_uniq' {
	replace `v' = . if id != 3
	local temp "se`v'"
	replace `temp' = . if id != 4 & id != 3.5
}

unab vars: se*
local newvars: subinstr local vars "se" "", all
foreach var of varlist `newvars' {
	local temp "se`var'"
	*replace `temp' = `temp'[_n-1] if missing(`temp') 
	tostring `temp', replace format(%9.3f) force
	replace `temp' = "[" + `temp' + "]" if id == 2 | id == 4 | id == 6
	replace `temp' = "(" + `temp' + ")" if id == 1.5 | id == 3.5 | id == 5.5
	tostring `var', replace format(%9.3f) force
	replace `var' = `temp' if `var' == "."
	drop `temp'
	replace `var' = "." if `var' == "(.)" | `var' == "[.]"
}

unab varlistf: *_F_I*
foreach v of varlist `varlistf' {
	replace `v' = . if id != 9
	tostring `v', replace format(%9.1f) force
	replace `v' = "" if `v' == "."
	local temp: subinstr local v "F_I_" "", all
	replace `temp' = `v' if `temp' == "." | `temp' == ""
	drop `v'
}
unab varlistf: *_F_*
foreach v of varlist `varlistf' {
	replace `v' = . if id != 8
	tostring `v', replace format(%9.1f) force
	replace `v' = "" if `v' == "."
	local temp: subinstr local v "F_" "", all
	replace `temp' = `v' if `temp' == "." | `temp' == ""
	drop `v'
}
forval i = 2/3 {
unab varlistn: *N`i'* 
foreach v of varlist `varlistn' {
	replace `v' = . if id != 10
	tostring `v', replace format(%9.0f) force
	replace `v' = "" if `v' == "."
	local temp: subinstr local v "N`i'" "b`i'", all
	replace `temp' = `v' if `temp' == "." | `temp' == ""
	drop `v'
}
}

unab varlist2: *I_b2*
foreach v of varlist `varlist2' {
	local temp: subinstr local v "I_" "", all
	replace `temp' = `v' if `temp' == ""
	drop `v'
	}



unab varlist: *b*
foreach v of varlist `varlist' {
	replace `v' = "" if `v' == "."
	}
	


gen v = "OutputComp" if id == 1
replace v = "InputAccess" if id == 3
replace v = "InputAccessIV" if id == 5
replace v = "F-Stat IV1" if id == 8
replace v = "F-Stat IV2" if id == 9
replace v = "Observations" if id == 10
replace v = "Year FE" if id == 11
replace v = "Industry FE" if id == 12
replace v = "Industry-Year FE" if id == 13

unab varlist: *_y
foreach v of varlist `varlist' {
	replace `v' = "Yes" if id == 11
	replace `v' = "No" if id == 12 | id == 13
	}

unab varlist: *_yn
foreach v of varlist `varlist' {
	replace `v' = "Yes" if id == 11 | id == 12
	replace `v' = "No" if  id == 13
	}

unab varlist: *_ycn
foreach v of varlist `varlist' {
	replace `v' = "Yes" if id == 13
	replace `v' = "No" if id == 11 | id == 12
	}

unab varlist: diff_tfpr_b*
foreach v of varlist `varlist' {
	la var `v' "Revenue TFP"
	}
unab varlist: dif_d_*
foreach v of varlist `varlist' {
	la var `v' "Mean Demand"
	}
unab varlist: dif_a_*
foreach v of varlist `varlist' {
	la var `v' "Mean TFPQ"
	}
unab varlist: dif_cov_*
foreach v of varlist `varlist' {
	la var `v' "Intensitve Margin Reallocation"
	}
unab varlist: dif_var_*
foreach v of varlist `varlist' {
	la var `v' "Scope Effect"
	}
unab varlist: adding_*
foreach v of varlist `varlist' {
	la var `v' "New Product"
	}
unab varlist: drop_*
foreach v of varlist `varlist' {
	la var `v' "Product Dropping"
	}
foreach var in y yn ycn {
preserve
drop if id > 4 & id < 7
texsave v diff_tfpr_b2_`var' dif_d_bar_b2_`var' dif_a_bar_b2_`var' dif_cov_stay_b2_`var' dif_var_adj_b2_`var' adding_term_b2_`var' drop_term_b2_`var' using "$tables/decomptable_`samplenamefile'_2_`var'.tex", ///
	footnote("Note: SE in (.), Bootstrapped SEs in [.]") ///
	title("Decompositions: (" "`samplenametext'" ")") ///
	nofix replace location(h) varlab  marker(tab:bs) frag ///
	autonumber width(\textwidth)
restore
preserve
drop if id > 2 & id < 5 | id == 9
texsave v diff_tfpr_b3_`var' dif_d_bar_b3_`var' dif_a_bar_b3_`var' dif_cov_stay_b3_`var' dif_var_adj_b3_`var' adding_term_b3_`var' drop_term_b3_`var' using "$tables/decomptable_`samplenamefile'_3_`var'.tex", ///
	footnote("Note: SE in (.), Bootstrapped SEs in [.]") ///
	title("Decompositions: (" "`samplenametext'" ")") ///
	nofix replace location(h) varlab  marker(tab:bs) frag ///
	autonumber width(\textwidth)
restore
}
